package com.bac.rentmap.osmimport;

import org.xml.sax.InputSource;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;
import java.util.ArrayList;

/**
 *
 * @author Ivan Holub
 */
public class OsmImport {

    private static final String OSM_FILE = "D:/projects/rent-map/belarus.osm";
    private static final String HIGHWAYS_FILE = "highways.txt";
    private static final String BUILDINGS_FILE = "buildings.txt";

    //Minsk
    private static final OsmBbox BBOX = new OsmBbox("27.39741", "53.82871", "27.75825", "53.97496");

    public static void main(String [] args) throws Exception {
        System.out.println("start parse: " + new Date());
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        OsmContentHandler handler = new
                OsmContentHandler(BBOX);
        FileInputStream fis = new FileInputStream(OSM_FILE);
        InputSource input = new InputSource(fis);
        saxParser.parse(input, handler);
        System.out.println("end parse: " + new Date());

        System.out.println("nodes: " + handler.getNodes().size());
        List<OsmBuilding> buildings = handler.getBuildings();
        Map<String, OsmHighway> highwaysMap = handler.getHighwaysMap();
        System.out.println("buildings: " + buildings.size());
        System.out.println("highways: " + highwaysMap.size());

        writeBuildings(buildings);
        writeHighways(highwaysMap);

    }

    private static void writeBuildings(List<OsmBuilding> buildings) throws Exception {
        Collections.sort(buildings, new Comparator<OsmBuilding> () {
            public int compare(OsmBuilding o1, OsmBuilding o2) {
                int retValue = o1.getStreet().compareTo(o2.getStreet());
                if (retValue == 0) {
                    retValue = o1.getHousenumber().compareTo(o2.getHousenumber());
                }
                return retValue;
            }
        });

        FileOutputStream fos = new FileOutputStream(BUILDINGS_FILE);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
        for (OsmBuilding b : buildings) {
            out.write(b.getLat());
            out.write("\t");
            out.write(b.getLon());
            out.write("\t");
            out.write(b.getStreet());
            out.write("\t");
            out.write(b.getHousenumber());
            out.write("\n");
        }
        out.close();
    }

    private static void writeHighways(Map<String, OsmHighway> highwaysMap) throws Exception {
        List<OsmHighway> highways = new ArrayList<OsmHighway>(highwaysMap.values());

        Collections.sort(highways, new Comparator<OsmHighway> () {
            public int compare(OsmHighway o1, OsmHighway o2) {
                int retValue = o1.getName().compareTo(o2.getName());
                return retValue;
            }
        });

        FileOutputStream fos = new FileOutputStream(HIGHWAYS_FILE);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
        for (OsmHighway b : highways) {
            HighwayRange highwayRange = b.getHighwayRange();
            out.write(highwayRange.getMedianLat());
            out.write("\t");
            out.write(highwayRange.getMedianLon());
            out.write("\t");            
            out.write(b.getName());
            out.write("\n");
        }
        out.close();
    }
}